package com.wondertek.liveroom;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class FragmentLivePersonalMine extends Fragment {
	private View view = null;
	private PullToRefreshListView ptrListView = null;
	private ListView listView = null;
	private BaseAdapter adapter = null;
	private Class<?> c  = null;
	
	public FragmentLivePersonalMine(Class<?> c) {
		this.c = c;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.fragment_live_classify_new, null);
		initCtrl();
		return view;
	}
	
	private void initCtrl() {
		ptrListView = (PullToRefreshListView) view.findViewById(R.id.lv_ptr_live_cate);
		listView = ptrListView.getRefreshableView();
		setAdapter();
		(ptrListView).setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				new GetDataTask().execute();
			}
		});
		
		listView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//enter the activity
			}			
		});
	}
	
	private void setAdapter() {
		if (this.c.equals(MyLivePersonalMineListViewAdapter.class)) {
			adapter = new MyLivePersonalMineListViewAdapter(getActivity(), null);
		} else if (this.c.equals(MyLivePersonalSubscribeListViewAdapter.class)) {
			adapter = new MyLivePersonalSubscribeListViewAdapter(getActivity(), null);
		}
		
		if (adapter != null) {
			listView.setAdapter(adapter);
		}		
	}
	
//	public void setAdapterName(String str) {
//		if(str.equals("1")) {
//			Log.e("aaaaaaaaaaaaaaaaaaaaa", "fadMyLivePersonalMineListViewAdapterMyLivePersonalMineListViewAdapterMyLivePersonalMineListViewAdapter");
//			Log.e("aaaaaaaaaaaaaaaaaaaaa","aaa   " + getActivity());
//			adapter = new MyLivePersonalMineListViewAdapter(getActivity(), null);
//		}
//		
//		//adapter = new MyLivePersonalMineListViewAdapter(getActivity(), null);
//		listView.setAdapter(adapter);
//	}
	
	private class GetDataTask extends AsyncTask<Void, Void, String[]> {
		@Override
		protected String[] doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			}
			return null;
		}

		@Override
		protected void onPostExecute(String[] result) {
			// Do some stuff here
			// Call onRefreshComplete when the list has been refreshed.
			ptrListView.onRefreshComplete();
			super.onPostExecute(result);
		}
	}
}
